﻿using System;
using System.Linq;
using System.Text;
using BO.Infrastructure.Utils;
using Utilities.Entities;

namespace GUI.Controls.News
{
    public partial class ListNewsByTag : System.Web.UI.UserControl
    {
        private const string LiFormat =
                "<li>"
                + "{5}"
                + "<div class=\"scope\">"
                    + "<h2><a href=\"{0}\" title=\"{2}\" class=\"prop_title\">{1}</a></h2>"
                    + "<p class=\"prop_publishinfo\"><b>{3}</b></p>"
                    + "<p class=\"prop_desc\">{4}</p>"
                    + "{6}"
                + "</div>"
                + "</li>";

        private const string TagFormat = "<p class=\"prop_tags\">Tags: {0}</p>";

        private const string LinkFormat = "<a href=\"{0}\" title=\"{2}\">{1}</a>";
        private const string StrongLinkFormat = "<a href=\"{0}\" title=\"{2}\"><strong>{1}</strong></a>";

        private const int ImageWidth = 200;
        private const int ImageHeight = 150;

        internal bool DataBind(SearchResultItem[] searchResultItem, string boldTag)
        {
            if (searchResultItem != null && searchResultItem.Length > 0)
            {
                var sb = new StringBuilder();
                foreach (var item in searchResultItem)
                {
                    var avatar = PresentationUtils.Photo.BuildPhoto(
                        source: item.Avatar,
                        title: item.Title,
                        href: item.Url,
                        width: ImageWidth,
                        height: ImageHeight, lazyLoad: true);

                    var sbTags = BuildTags(item.TagItem, item.Tags, boldTag);
                    sbTags = string.IsNullOrEmpty(sbTags)
                                 ? string.Empty
                                 : string.Format(TagFormat, sbTags);

                    sb.Append(string.Format(
                            LiFormat,
                            item.Url,
                            item.Title,
                            item.EncodedTitle,
                            item.DistributionDate.ToString("dd/MM/yyyy HH:mm"),
                            item.InitSapo,
                            avatar,
                            sbTags
                        ));

                }
                ltrData.Text = sb.ToString();
                return true;
            }
            return false;
        }

        private string BuildTags(string[] tagsItems, string[] tagUrls, string boldTag)
        {
            if (tagsItems == null || !tagsItems.Any() || tagUrls == null || !tagsItems.Any())
                return string.Empty;
            var sb = new StringBuilder();
            for (var i = 0; i < tagsItems.Length; i++)
            {
                try
                {
                    sb.AppendFormat(tagsItems[i].Equals(boldTag, StringComparison.InvariantCultureIgnoreCase) ? StrongLinkFormat : LinkFormat, PresentationUtils.UrlBuilder.BuildTagUrl(tagUrls[i], false),
                                            tagsItems[i], PresentationUtils.EnCode(tagsItems[i])).Append(", ");
                }
                catch (IndexOutOfRangeException)
                {
                    break;
                }
            }
            return sb.ToString().TrimEnd(", ".ToArray());
        }
    }
}